import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../common.dart';

class resaleDateil extends StatefulWidget {
  resaleDateil({super.key});

  @override
  State<resaleDateil> createState() => _WebDetailState();
}

class _WebDetailState extends State<resaleDateil> {
  final uuid = Get.parameters['uuid'];
  final is_box = Get.parameters['is_box'];
  final RxMap productsData = {}.obs;
  final RxList boxInfo = [].obs;
  final RxInt _index = 0.obs;
  final RxMap indexItem = {}.obs;

  getDetail() {
    getData(url: '/packages/info/$uuid').then((res) {
      if (res != null) {
        productsData.value = res['info'];
      }
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getDetail();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: buildBackButton(context),
        backgroundColor: Color(0xffffffff),
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView(
              children: [
                Obx(
                  () => Stack(
                    children: [
                      Container(
                        margin: EdgeInsets.symmetric(horizontal: 15),
                        child: FractionallySizedBox(
                          widthFactor: 1,
                          child: Image.network(
                            '${productsData['thumb']}',
                            errorBuilder: (context, error, stackTrace) {
                              return SizedBox(width: double.infinity);
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(height: 15),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 15),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Obx(
                        () => Text(
                          '限量${productsData['sell_num']}份，流通${productsData['release_num']}份',
                          style: TextStyle(
                            fontSize: 14,
                            color: Color(0XFF1F0584),
                          ),
                        ),
                      ),
                      Obx(
                        () => Text(
                          '${productsData['title']}',
                          style: TextStyle(
                            fontSize: 22,
                            color: Color(0XFF0F172B),
                          ),
                        ),
                      ),
                      SizedBox(height: 20),
                      Row(
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '所在产链',
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Color(0XFF999999),
                                ),
                              ),
                              Obx(
                                () => Text(
                                  '${productsData['link_name']}',
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Color(0XFF0F172B),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          SizedBox(width: 100),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '发售时间',
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Color(0XFF999999),
                                ),
                              ),
                              Obx(
                                () => Text(
                                  '${productsData['created_at']}',
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Color(0XFF0F172B),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                      SizedBox(height: 20),
                      Row(
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '合约地址',
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Color(0XFF999999),
                                ),
                              ),
                              Obx(
                                () =>
                                    productsData.isNotEmpty &&
                                            productsData['link_address']
                                                .isNotEmpty
                                        ? Text(
                                          '${productsData['link_address'].substring(0, 3)}...${productsData['link_address'].substring(14, 18)}',
                                          style: TextStyle(
                                            fontSize: 14,
                                            color: Color(0XFF0F172B),
                                          ),
                                        )
                                        : Text('...'),
                              ),
                            ],
                          ),
                          SizedBox(width: 100),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '支付方式',
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Color(0XFF999999),
                                ),
                              ),
                              Obx(
                                () => Text(
                                  '${productsData['pay_type']}',
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Color(0XFF0F172B),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                      Divider(color: Color(0XFFDDDDDD), height: 30),
                      Row(
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '获得时间',
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Color(0XFF999999),
                                ),
                              ),
                              Obx(
                                () => Text(
                                  '${productsData['package_sku_created_at']}',
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Color(0XFF0F172B),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          SizedBox(width: 100),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '免费周期(天)',
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Color(0XFF999999),
                                ),
                              ),
                              Obx(
                                () => Text(
                                  '${productsData['free_days']}',
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Color(0XFF0F172B),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                      Divider(color: Color(0XFFDDDDDD), height: 30),
                      Text(
                        '藏品简介',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(height: 10),
                      Obx(
                        () => Column(
                          children: [
                            if (productsData.isNotEmpty)
                              for (var item in productsData['images'])
                                FractionallySizedBox(
                                  widthFactor: 1,
                                  child: Image.network(item),
                                ),
                          ],
                        ),
                      ),
                      SizedBox(height: 10),
                      Text(
                        '购买须知',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(height: 5),
                      Text(
                        '数字藏品交易仅限于18周岁(不含)以上、60周岁(含)以下，并已完成实名认证的中国大陆用户购买。由于不同属性的数字艺术有不同的二次交易规则，请您仔细阅读并同意相关规则后再进行相应操作，以避免不必要的损失。 请注意，一旦数字藏品完成出售，不支持退货在购买之前，请仔细考虑您的购买决策。根据我国相关的法律要求，特定条件下的数字藏品的二次交易不支持任何形式的变相炒作。我们坚决反对数字藏品炒作、场外交易、欺诈，以及其他非法侵权行为。请您甄别上述涉嫌网络欺诈的行为并了解相关风险。',
                        style: TextStyle(
                          fontSize: 12,
                          color: Color(0XFF999999),
                        ),
                      ),
                      SizedBox(height: 15),
                    ],
                  ),
                ),
              ],
            ),
          ),
          Obx(
            () =>
                productsData['meta_chain'] == 1 ||
                        productsData['is_hidden'] == 1
                    ? Row(
                      children: [
                        // productsData['is_hidden'] == 1
                        //     ?
                        if (productsData['is_hidden'] == 1)
                          Expanded(
                            child: GestureDetector(
                              onTap: () {
                                Get.dialog(
                                  AlertDialog(
                                    backgroundColor: Colors.transparent,
                                    content: Stack(
                                      children: [
                                        Image.asset(
                                          'static/tan.png',
                                          width: 250,
                                          fit: BoxFit.fitWidth,
                                        ),
                                        Positioned(
                                          top: 80,
                                          left: 0,
                                          right: 0,
                                          child: Align(
                                            child: Column(
                                              children: [
                                                Text(
                                                  '是否打开盲盒',
                                                  style: TextStyle(
                                                    fontSize: 24,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                                SizedBox(height: 15),
                                                Text(
                                                  '此操作不可逆',
                                                  style: TextStyle(
                                                    fontSize: 12,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                                GestureDetector(
                                                  onTap: () {
                                                    getData(
                                                      url:
                                                          '/packages/open/$uuid',
                                                    ).then((res) {
                                                      if (res != null) {
                                                        Get.toNamed(
                                                          '/web_tabs/resale/open_box?thumb=${res['thumb']}&title=${res['title']}&updated_at=${res['updated_at']}',
                                                        );
                                                      }
                                                    });
                                                  },
                                                  child: Container(
                                                    height: 50,
                                                    margin: EdgeInsets.only(
                                                      top: 50,
                                                      bottom: 30,
                                                    ),
                                                    alignment: Alignment.center,
                                                    width: 210,
                                                    decoration: BoxDecoration(
                                                      color: Color(0XFF0F172B),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                            50,
                                                          ),
                                                    ),
                                                    child: Text(
                                                      '确认',
                                                      style: TextStyle(
                                                        fontSize: 16,
                                                        color: Color(
                                                          0XFFFFFFFF,
                                                        ),
                                                      ),
                                                    ),
                                                  ),
                                                ),
                                                GestureDetector(
                                                  onTap: () => {Get.back()},
                                                  child: Text(
                                                    '取消',
                                                    style: TextStyle(
                                                      fontSize: 18,
                                                      fontWeight:
                                                          FontWeight.bold,
                                                    ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              },
                              child: Container(
                                alignment: Alignment.center,
                                margin: EdgeInsets.symmetric(
                                  horizontal: 15,
                                  vertical: 10,
                                ),
                                height: 50,
                                decoration: BoxDecoration(
                                  color: Color(0XFF542AFF),
                                  borderRadius: BorderRadius.circular(60),
                                ),
                                child: Text(
                                  '开盒',
                                  style: TextStyle(
                                    fontSize: 18,
                                    color: Color(0XFFFFFFFF),
                                  ),
                                ),
                              ),
                            ),
                          ),

                        if (productsData['is_hidden'] != 1 &&
                            productsData['is_by'] == 1 &&
                            productsData['status'] == 1 &&
                            productsData['hosting'] == 0)
                          Expanded(
                            child: GestureDetector(
                              onTap: () {
                                Get.toNamed(
                                  '/web_tabs/resale/sale?img=${productsData['thumb']}&nftId=${productsData['nftId']}&title=${productsData['title']}&id=${productsData['id']}&sell_num=${productsData['sell_num']}&examine_max_ratio=${productsData['examine_max_ratio']}&examine_min_ratio=${productsData['examine_min_ratio']}&brokerage_ratio=${productsData['brokerage_ratio']}&edition_tax_ratio=${productsData['edition_tax_ratio']}',
                                );

                                // Get.toNamed(
                                //     '/web_tabs/resale/resale?img=${productsData['thumb']}&nftId=${productsData['nftId']}&title=${productsData['title']}&id=${productsData['id']}&sell_num=${productsData['sell_num']}&examine_max_ratio=${productsData['examine_max_ratio']}&examine_min_ratio=${productsData['examine_min_ratio']}&brokerage_ratio=${productsData['brokerage_ratio']}&edition_tax_ratio=${productsData['edition_tax_ratio']}');
                              },
                              child: Container(
                                alignment: Alignment.center,
                                margin: EdgeInsets.symmetric(
                                  horizontal: 15,
                                  vertical: 10,
                                ),
                                height: 50,
                                decoration: BoxDecoration(
                                  color: Color(0XFF542AFF),
                                  borderRadius: BorderRadius.circular(60),
                                ),
                                child: Text(
                                  '转售',
                                  style: TextStyle(
                                    fontSize: 18,
                                    color: Color(0XFFFFFFFF),
                                  ),
                                ),
                              ),
                            ),
                          ),

                        if (productsData['meta_chain'] == 1 &&
                            productsData['status'] == 1 &&
                            productsData['hosting'] == 0)
                          Expanded(
                            child: GestureDetector(
                              onTap: () {
                                if (productsData['is_show'] == 0) {
                                  return;
                                }
                                Get.toNamed(
                                  '/web_tabs/resale/resale?img=${productsData['thumb']}&nftId=${productsData['nftId']}&title=${productsData['title']}&id=${productsData['id']}&sell_num=${productsData['sell_num']}&examine_max_ratio=${productsData['examine_max_ratio']}&examine_min_ratio=${productsData['examine_min_ratio']}&brokerage_ratio=${productsData['brokerage_ratio']}&edition_tax_ratio=${productsData['edition_tax_ratio']}',
                                );
                              },
                              child: Container(
                                alignment: Alignment.center,
                                margin: EdgeInsets.symmetric(
                                  horizontal: 15,
                                  vertical: 10,
                                ),
                                height: 50,
                                decoration: BoxDecoration(
                                  color:
                                      productsData['is_show'] == 0
                                          ? Color(0XFF999999)
                                          : Color(0XFF0F172B),
                                  borderRadius: BorderRadius.circular(60),
                                ),
                                child: Text(
                                  productsData['hosting'] > 0
                                      ? '托管费 (￥${productsData['hosting']})'
                                      : '寄售',
                                  style: TextStyle(
                                    fontSize: 18,
                                    color: Color(0XFFFFFFFF),
                                  ),
                                ),
                              ),
                            ),
                          ),

                        if (productsData['hosting'] != 0 &&
                            productsData['status'] != 5)
                          Expanded(
                            child: GestureDetector(
                              onTap: () {
                                postData(
                                  url: '/packages/pay',
                                  data: {
                                    'uuid': uuid,
                                    'total': '1',
                                    'url': "back.html?web=tuoguan",
                                    'wallet': "HF",
                                  },
                                ).then((res) async {
                                  if (res != null) {
                                    if (kIsWeb) {
                                      final uur = Uri.parse(
                                        '${res['pay_url']}',
                                      );
                                      launchUrl(
                                        uur,
                                        webOnlyWindowName: '_self',
                                      );
                                    } else {
                                      final result = await Get.toNamed(
                                        '/webview?url=${res['pay_url']}&back=suuy&h=${Random().nextInt(100)}',
                                      );
                                      getDetail();
                                    }
                                  }
                                });
                              },
                              child: Container(
                                alignment: Alignment.center,
                                margin: EdgeInsets.symmetric(
                                  horizontal: 15,
                                  vertical: 10,
                                ),
                                height: 50,
                                decoration: BoxDecoration(
                                  color:
                                      productsData['is_show'] == 0
                                          ? Color(0XFF999999)
                                          : Color(0XFF0F172B),
                                  borderRadius: BorderRadius.circular(60),
                                ),
                                child: Text(
                                  productsData['hosting'] > 0
                                      ? '支付托管费 (￥${productsData['hosting']})'
                                      : '寄售',
                                  style: TextStyle(
                                    fontSize: 18,
                                    color: Color(0XFFFFFFFF),
                                  ),
                                ),
                              ),
                            ),
                          ),

                        if (productsData['meta_chain'] == 1 &&
                            productsData['status'] == 5)
                          Expanded(
                            child: GestureDetector(
                              onTap: () {
                                Get.dialog(
                                  AlertDialog(
                                    backgroundColor: Colors.transparent,
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(5),
                                    ),
                                    content: Stack(
                                      children: [
                                        Image.asset(
                                          'static/tan.png',
                                          width: 255,
                                          fit: BoxFit.fitWidth,
                                        ),
                                        Positioned(
                                          top: 80,
                                          right: 0,
                                          left: 0,
                                          child: Column(
                                            children: [
                                              Text(
                                                '提示',
                                                style: TextStyle(
                                                  fontSize: 24,
                                                  color: Color(0XFF0F172B),
                                                ),
                                              ),
                                              SizedBox(height: 10),
                                              Text(
                                                '确定要取消寄售吗',
                                                style: TextStyle(
                                                  fontSize: 14,
                                                  color: Color(0XFF0F172B),
                                                ),
                                              ),
                                              SizedBox(height: 30),
                                              GestureDetector(
                                                onTap: () {
                                                  postData(
                                                    url:
                                                        '/resale/close/${productsData['resale_uuid']}',
                                                  ).then((res) {
                                                    if (res != null) {
                                                      Get.offNamed('/');
                                                      showTost('取消成功');
                                                    }
                                                  });
                                                },
                                                child: Container(
                                                  width: 210,
                                                  height: 45,
                                                  decoration: BoxDecoration(
                                                    color: Color(0XFF0F172B),
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                          45,
                                                        ),
                                                  ),
                                                  alignment: Alignment.center,
                                                  child: Text(
                                                    '确认',
                                                    style: TextStyle(
                                                      fontSize: 18,
                                                      color: Color(0XFFFFFFFF),
                                                    ),
                                                  ),
                                                ),
                                              ),
                                              SizedBox(height: 10),
                                              GestureDetector(
                                                onTap: () {
                                                  Get.back();
                                                },
                                                child: Text(
                                                  '取消',
                                                  style: TextStyle(
                                                    fontSize: 18,
                                                    color: Color(0XFF0F172B),
                                                  ),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              },
                              child: Container(
                                alignment: Alignment.center,
                                margin: EdgeInsets.symmetric(
                                  horizontal: 15,
                                  vertical: 10,
                                ),
                                height: 50,
                                decoration: BoxDecoration(
                                  color:
                                      productsData['is_show'] == 0
                                          ? Color(0XFF999999)
                                          : Color(0XFF0F172B),
                                  borderRadius: BorderRadius.circular(60),
                                ),
                                child: Text(
                                  '取消寄售',
                                  style: TextStyle(
                                    fontSize: 18,
                                    color: Color(0XFFFFFFFF),
                                  ),
                                ),
                              ),
                            ),
                          ),
                      ],
                    )
                    : SizedBox(),
          ),
        ],
      ),
    );
  }
}
